package com.stenio.fancat.service;

import com.stenio.fancat.domain.behavior.BehaviorOperationImage;
import com.stenio.fancat.mapper.Behavior.BehaviorOperationImageMapper;
import com.stenio.fancat.mapper.user.UserRelationshipFomIdMapper;
import org.apache.commons.collections.CollectionUtils;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.scheduling.quartz.QuartzJobBean;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

/**
 * @author liuan
 * @create 2019-01-24 00:52
 */
@Component
public class Quartz {

    @Autowired
    private BehaviorOperationImageMapper behaviorOperationImageMapper;

    @Autowired
    private OssService ossService;

    @Autowired
    private UserRelationshipFomIdMapper userRelationshipFomIdMapper;

    @Scheduled(cron = "0 0/5 * * * ?")
    protected void executeInternal() {

        List<BehaviorOperationImage> behaviorOperationImages = behaviorOperationImageMapper.selectImagesByStatus(1);
        if(CollectionUtils.isNotEmpty(behaviorOperationImages)) {
            List<String> imageNames = new ArrayList<>();
            for(BehaviorOperationImage b : behaviorOperationImages) {
                List<String> strings = ossService.ckeckUrl(b.getImage());
                if(!strings.contains("block")){
                    behaviorOperationImageMapper.updateImageStatus(b.getId());
                }else{
                    behaviorOperationImageMapper.deleteImageById(b.getId());
                    String imageName = ossService.splitName(b.getImage());
                    imageNames.add(imageName);
                }
            }
            if(CollectionUtils.isNotEmpty(imageNames)) {
                ossService.deleteFileList(imageNames);
                imageNames.clear();
            }
        }

    }

    //每4小时执行1次
    @Scheduled(cron = "0 0 0/4 * * ?")
    public void delFormId(){
        userRelationshipFomIdMapper.delFormIdByUid();
    }

}
